﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace ExcelControlManager
{
    public partial class OutputInfoForm : Form
    {
        public delegate void delegUpdateLabelText(string labelText);
        public delegate void delegShowMessageBox(string Message, int index);
        List<FileInfo> TargetFiles = new List<FileInfo>();
        long lRecordNum = 0;
        string strKeyWord;
        string strOutputFileName = @"\Output\outputdata.xlsx";

        public OutputInfoForm(List < FileInfo > files, string keyword)
        {
            InitializeComponent();
            TargetFiles = files;
            strKeyWord = keyword;
        }

        private void OutputInfoForm_Load(object sender, EventArgs e)
        {
            label4.Text = System.Windows.Forms.Application.StartupPath + strOutputFileName;
            label5.Text = "0/" + TargetFiles.Count.ToString();
            label2.Text = "正在导出关键字相关记录...";
            this.Text = "正在导出...";
            Thread exceloperater = new Thread(ExcelOperater);
            exceloperater.IsBackground = true;
            exceloperater.Start();
        }

        private void ExcelOperater()
        {
            ExcelHelper excelHelper = new ExcelHelper();
            DataTable dtSource = null;
            DataTable dtOutput = null;
            DataRowView[] drv = null;
            lRecordNum = 0;
            for (int i = 0; i < TargetFiles.Count; i++)
            {
                dtSource = null;
                dtOutput = null;
                dtSource = excelHelper.ExcelToDataTable(TargetFiles[i].FullName.ToString(), null, false);
                if (dtSource == null)
                    continue;
                dtOutput = dtSource.Clone();
                DataView dv = dtSource.DefaultView;
                for (int j = 1; j <= dtSource.Columns.Count; j++)
                {
                    drv = null;
                    dv.Sort = "Column" + j.ToString() + " ASC";
                    drv = dv.FindRows(strKeyWord);
                    foreach (DataRowView rv in drv)
                    {
                        dtOutput.ImportRow(rv.Row);//复制行数据到新表
                    }
                }
                foreach (DataRow dr in dtOutput.Rows)
                {
                    excelHelper.AddOneRowToExcel(dr,
                        System.Windows.Forms.Application.StartupPath + strOutputFileName,
                        null);
                    lRecordNum++;
                }
                string labeltext = (i + 1).ToString() + "/" + TargetFiles.Count.ToString();
                this.Invoke(new delegUpdateLabelText(UpdateLabelText), labeltext);
            }
            this.Invoke(new delegShowMessageBox(ShowMessageBox), "成功导出" + lRecordNum.ToString() + "条记录", 1);
        }

        private void UpdateLabelText(string labelText)
        {
            label5.Text = labelText;
        }

        private void ShowMessageBox(string Message, int index)
        {
            switch(index)
            {
            case 1:
                MessageBox.Show(Message, "导出结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
                break;
            case -1:
                MessageBox.Show(Message, "导出结果", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                break;
            default:
                break;
            }
        }
    }
}
